import { fetchList as languageList } from '@/api/languages'
import areaApi from '@/api/area/area'
import { fetchTagAll } from '@/api/tag'
import { fetchCategoryAll, fetchCategoryTree } from '@/api/category'
import { fetchIngredientAll } from '@/api/ingredients'
import { fetchBenefitAll } from '@/api/benefits'
import articleCategoryApi from '@/api/article/articlecategory'

// import { fetchSupplementAll } from '@/api/supplements'
// import { fetchProductMmcAll } from '@/api/productOmc'
import i18n from '@/lang/index'

const locale = {
  state: {
    allareas: [],
    areas: [],
    languages: [],
    categories: [],
    categoryTree: [],
    tags: [],
    alltag: [],
    ingredients: [],
    benefits: [],
    // supplements: [],
    productMmcAlls: [],
    articleCates: []
  },
  mutations: {
    SET_ALLAREAS: (state, log) => {
      state.allareas = log
    },
    SET_AREAS: (state, log) => {
      state.areas = log
    },
    SET_ALLTAG: (state, log) => {
      state.alltag = log
    },
    SET_CATEGORIES: (state, cate) => {
      state.categories = cate
    },
    SET_CATEGORYTREE: (state, cate) => {
      state.categoryTree = cate
    },
    SET_TAGS: (state, tag) => {
      state.tags = tag
    },
    SET_LANGUAGES: (state, log) => {
      state.languages = log
    },
    SET_INGREDIENTS: (state, data) => {
      state.ingredients = data
    },
    SET_BENEFITS: (state, data) => {
      state.benefits = data
    },
    SET_ARTICLECATE: (state, data) => {
      state.articleCates = data
    },
    // SET_SUPPLEMENTS: (state, data) => {
    //   state.supplements = data
    // },
    // SET_PRODUCTMMCALL: (state, data) => {
    //   state.productMmcAlls = data
    // }
  },
  actions: {
    setLanguages({ commit }) {
      return new Promise((resolve, reject) => {
        languageList().then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push({ abbr: data[i].abbr, name: data[i].name })
            }
          }
          commit('SET_LANGUAGES', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAllAreas({ commit }) {
      return new Promise((resolve, reject) => {
        areaApi.areaList({ 'isAll': 1 }).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_ALLAREAS', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAreas({ commit }) {
      return new Promise((resolve, reject) => {
        areaApi.areaList({}).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_AREAS', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAllTag({ commit }) {
      return new Promise((resolve, reject) => {
        fetchTagAll({lang: i18n.locale}).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_ALLTAG', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAllCategory({ commit }, query) {
      return new Promise((resolve, reject) => {
        fetchCategoryAll(query).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_CATEGORIES', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setCategoryTree({ commit }) {
      return new Promise((resolve, reject) => {
        fetchCategoryTree({lang: i18n.locale}).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            /* for (i in data) {
              list.push(data[i])
            } */
            console.log(data)
            commit('SET_CATEGORYTREE', data)
          }
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAllIngredient({ commit }) {
      return new Promise((resolve, reject) => {
        fetchIngredientAll({lang: i18n.locale}).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_INGREDIENTS', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setAllBenefit({ commit }) {
      return new Promise((resolve, reject) => {
        fetchBenefitAll({lang: i18n.locale}).then(response => {
          var i = 0
          var list = []
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          commit('SET_BENEFITS', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    setArticleCategory({ commit }) {
      return new Promise((resolve, reject) => {
        articleCategoryApi.fetchCategoryTree().then(response => {
          var i = 0
          var list = []
          console.log('res', response.data)
          if (parseInt(response.data.code) == 0) {
            var data = response.data.data
            for (i in data) {
              list.push(data[i])
            }
          }
          console.log(list)
          commit('SET_ARTICLECATE', list)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // setAllSupplement({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     fetchSupplementAll({lang: i18n.locale}).then(response => {
    //       var i = 0
    //       var list = []
    //       if (parseInt(response.data.code) == 0) {
    //         var data = response.data.data
    //         for (i in data) {
    //           list.push(data[i])
    //         }
    //       }
    //       commit('SET_SUPPLEMENTS', list)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
    // setAllProductMmc({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     fetchProductMmcAll({lang: i18n.locale}).then(response => {
    //       var i = 0
    //       var list = []
    //       if (parseInt(response.data.code) == 0) {
    //         var data = response.data.data
    //         for (i in data) {
    //           list.push(data[i])
    //         }
    //       }
    //       commit('SET_PRODUCTMMCALL', list)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // }  
  }
}

export default locale
